/*
gobanjs - html5 SGF viewer for the Game of Go 

Copyright (C) 2013  Daniel Cioata
Visit http://www.govortex.ro/ for documentation, updates and examples.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/    

"use strict"

function SGoStage(config)
{
    this.config=config;
    
    this.context=null;
    this.canvas=null;
    this.pointx=null;
    this.pointx=null;    
    this.whiteImg=null;
    this.blackImg=null;    
    this.boardcolor=null;
        
}

SGoStage.prototype.init=function()
{
    var i;
    this.canvas=this.config.view.goban_canvas;
    
    if (this.canvas.getContext)
    {
        this.context = this.canvas.getContext('2d');
    }
    else
    {
        console.error("Your browser has no support for canvas");
        return false;
    }    
    
    this.createPoints();
    
    this.boardcolor='rgb(221,179,92)';
    this.context.textBaseline = "middle";
    this.context.textAlign    = "center";
    this.context.font         = 'bold 12px sans-serif';
    
    return true;
}

SGoStage.prototype.checkBounds=function(x,y)
{
    if(isNaN(x) || isNaN(y))
    {
        console.warn("Invalid x,y coordinates",x,y);
        return false;
    }
    if((x<0) || (x >=this.pointx.length) || (y<0) || (y >=this.pointy.length))
    {
         console.warn("Invalid x,y coordinates",x,y);         
         return false;
    }
    return true;
}

SGoStage.prototype.createPoints=function()
{
    var i, w, h, ssize;
    
    w = this.config.gridsize.width;
    h = this.config.gridsize.height;         
    ssize = this.config.stonesize;         
    console.log(ssize);
    this.pointx=new Array(w);
    this.pointy=new Array(h);
    
    for(i=0;i<w;i++)
        this.pointx[i] = Math.round(ssize+i*ssize) + 0.5;
    for(i=0;i<h;i++)
        this.pointy[i] = Math.round(ssize+i*ssize) + 0.5;
          
    this.createStone('white',ssize);
    this.createStone('black',ssize);  
}

SGoStage.prototype.createStone=function(color,ssize)
{
    var canvas,context,grad,radius,img,__this;       
    if(img!=null)
        return img;
             
    canvas = document.createElement('canvas');
    
    if(!canvas.getContext)
        return null;
    
    canvas.width  = ssize+2;
    canvas.height = ssize+2;
    context = canvas.getContext('2d');
    context.clearRect(0,0,canvas.width,canvas.height);
    
    radius=(ssize-1)/2;

    grad = context.createRadialGradient(radius/1.5, radius/1.5, 2,
        radius/1.5, radius/1.5, radius/1.2);
        
    if(color=='white')
    {
        grad.addColorStop(0, "#FFFFFF");
        grad.addColorStop(1, "#DDDDDD");

    }
    else if(color=='black')
    {
        grad.addColorStop(0, "#808080");
        grad.addColorStop(1, "#000000");
    }
            
    context.beginPath();
    context.fillStyle = 'rgba(0, 0, 0, 0.5)';
    context.arc(radius+2, radius+2, radius, 0, 2 * Math.PI, false);
    context.fill();
    context.closePath();
    
    context.beginPath();
    context.fillStyle = grad;
    context.arc(radius, radius, radius-0.5, 0, 2 * Math.PI, false);
    context.fill();
    context.closePath();
            
    img=new Image();                
    __this=this;
    img.onload=function()
    {                        
        img.style.width=ssize+2;
        img.style.height=ssize+2;
        if(color=='white')
            __this.whiteImg=this;           
        else    
            __this.blackImg=this;           
            
        if((__this.whiteImg!=null) && (__this.blackImg!=null))
        {
            __this.config.view.update();                
        }
    };
    img.src=canvas.toDataURL('image/png');           
}

SGoStage.prototype.drawGrid=function()
{
    var i, sx, ex, sy, ey;

    if(this.context==null)
        return false;

    this.context.fillStyle = this.boardcolor;
    this.context.fillRect (0,0,this.canvas.width,this.canvas.height);
    this.context.strokeStyle = '#000000';       
    
    this.context.beginPath();    
    
    sx=(this.config.bounds.top === 0) ? 0 : 1;
    ex=(this.config.bounds.bottom === this.config.boardsize) ? 0 : 1;
        
    for(i=sx;i<this.config.gridsize.height-ex;i++)
    {        
        this.context.moveTo(this.pointx[0],this.pointy[i]);
        this.context.lineTo(this.pointx[this.config.gridsize.width-1],this.pointy[i]);

    }
    
    sy=(this.config.bounds.left === 0) ? 0 : 1;
    ey=(this.config.bounds.right === this.config.boardsize) ? 0 : 1;
    
    for(i=sy;i<this.config.gridsize.width-ey;i++)
    {
        this.context.moveTo(this.pointx[i],this.pointy[0]);
        this.context.lineTo(this.pointx[i],this.pointy[this.config.gridsize.height-1]);
    }
    
    this.context.stroke();    
}

SGoStage.prototype.getX=function(x)
{
    if( x >= this.pointx[x].length)
    {
        console.log("Stage point x out of range", x);
        return 0;        
    }
    return this.pointx[x]-this.config.stonesize/2;
}

SGoStage.prototype.getY=function(y)
{    
    if( y >= this.pointy[y].length)
    {
        console.log("Stage point y out of range", y);
        return 0;        
    }
    return this.pointy[y]-this.config.stonesize/2;
}

SGoStage.prototype.drawStone=function(color, x, y)
{
    var x1,y1;

    if(this.context==null)
        return false;

    if(this.checkBounds(x,y)==false)
    {
        return false;
    }
    
    x1=this.pointx[x]-this.config.stonesize/2;
    y1=this.pointy[y]-this.config.stonesize/2;

    if((color==SGFProp.ID_B) && (this.blackImg!=null) )
    {            
        this.context.drawImage(this.blackImg,x1,y1);
    }
    else if((color==SGFProp.ID_W) && (this.whiteImg!=null) )
    {
        this.context.drawImage(this.whiteImg,x1,y1);    
    } 
    return true;
}

SGoStage.prototype.drawStoneIndex=function(index,color,x,y)
{
    if(this.checkBounds(x,y)==false)
        return false;

    this.context.fillStyle = color;
    this.context.fillText(""+index, this.pointx[x], this.pointx[y]);    
}

SGoStage.prototype.drawHandi=function(x,y)
{
    if(this.context==null)
        return false;

    this.context.fillStyle = '#000';
    this.context.beginPath();
    this.context.arc(this.pointx[x],this.pointx[y], 3, 0, Math.PI*2, false);
    this.context.fill();
    this.context.closePath();

    return true;
}




